#include "PortListWidget.h"
#include "ui_PortListWidget.h"
#include "AnimatedButton.h"

#include <QtSerialPort/QSerialPort>
#include <QSerialPortInfo>
#include <QtConcurrent/QtConcurrent>
#include <QComboBox>

PortListWidget::PortListWidget(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::PortListWidget)
{
	ui->setupUi(this);

	qRegisterMetaType<PortConnectInfo>("PortConnectInfo");

	_model = new ConnectItemModel(this);
	ui->connectTableView->setModel(_model);

	ui->portRefresh->setButtonIcon(":/images/refresh1.png");
	ui->portRefresh->setAnimation(":/images/refresh1.gif");
	ui->portRefresh->showButton(true);

	connect(ui->portRefresh, SIGNAL(clicked()), SLOT(onUpdateBtn()));
}

PortListWidget::~PortListWidget()
{
	delete ui;
}

void PortListWidget::onUpdateBtn()
{
	ui->portRefresh->showButton(false);
	_ports.clear();
	ui->connectComboBox->clear();
	EnumeratePorts* _thread = new EnumeratePorts(this);
	connect(_thread,SIGNAL(finished()),_thread,SLOT(deleteLater()));
	connect(_thread, SIGNAL(finished()),SLOT(onFinished()));
	connect(_thread, SIGNAL(portAdded(PortConnectInfo)),SLOT(addPortInfo(PortConnectInfo)),Qt::QueuedConnection);
	_thread->setWidget(this);
	_thread->start();
	//ui->portRefresh->showButton(true);
}

void PortListWidget::addPortInfo(PortConnectInfo info)
{
	_ports.append(info);
	_model->reloadData(_ports);
	ui->connectComboBox->insertItem(ui->connectComboBox->count(), info.name);
}

void PortListWidget::onFinished()
{
	ui->portRefresh->showButton(true);
}

/*void PortListWidget::updateList()
{
	QSerialPortInfo info;
	QList<QSerialPortInfo> list = info.availablePorts();

	_ports.clear();
	foreach(QSerialPortInfo s, list){
		QString name = s.portName();
		bool busy = s.isBusy();
		QString com = "";
		if(!busy){
			QSerialPort port(s);
			port.setBaudRate(QSerialPort::Baud19200);
			port.open(QSerialPort::ReadWrite);
			port.write("hello");
			bool read = port.waitForReadyRead(1000);
			if(read) com = port.readAll();
			port.close();
		}
		_ports.append(PortConnectInfo(name, !busy, com));

		_model->reloadData(_ports);
	}
}*/

//////////////////////////////////////////////////////////////

EnumeratePorts::EnumeratePorts(QObject* parent) :
	QThread(parent),
	_isRunning(true),
	_widget(NULL)
{
}

void EnumeratePorts::stop()
{
	_isRunning = false;
}

void EnumeratePorts::setWidget(PortListWidget* widget)
{
	_widget = widget;
}

void EnumeratePorts::run()
{
	QSerialPortInfo info;
	QList<QSerialPortInfo> list = info.availablePorts();

	foreach(QSerialPortInfo s, list){
		QString name = s.portName();
		bool busy = s.isBusy();
		QString com = "";
		if(!busy){
			QSerialPort port(s);
			port.setBaudRate(QSerialPort::Baud19200);
			port.open(QSerialPort::ReadWrite);
			port.write("hello");
			bool read = port.waitForReadyRead(1000);
			if(read) com = port.readAll();
			port.close();
		}
		emit portAdded(PortConnectInfo(name, !busy, com));
	}
}
